package com.ruiysoft.controller.supplier;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruiysoft.entity.supplier.Supplier;
import com.ruiysoft.entity.supplier.SupplierDTO;
import com.ruiysoft.pub.core.Result;
import com.ruiysoft.pub.core.ResultGenerator;
import com.ruiysoft.service.supplier.SupplierService;
import com.ruiysoft.service.sys.clent.LoginContextUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @author fanyl5
 */
@Slf4j
@Api(value = "Supplier", description = "供应商API")
@RestController
@RequestMapping(value = "/supplier")
public class SupplierController {

    private final SupplierService supplierService;

    public SupplierController(SupplierService supplierService) {
        this.supplierService = supplierService;
    }

    /**
     * 插入供应商信息
     *
     * @param supplierDTO
     * @return
     */
    @PostMapping("/insert")
    public Result save(@RequestBody SupplierDTO supplierDTO) {
        if (ObjectUtils.isEmpty(supplierDTO)) {
            return ResultGenerator.genFailResult("数据不能为空");
        }
        Supplier supplier = new Supplier();
        BeanUtils.copyProperties(supplierDTO, supplier);
        supplier.setCreateDate(LocalDateTime.now());
        Long shopId = LoginContextUtil.instance().getEntryMicroShop().getId();
        supplier.setShopId(shopId);

        return ResultGenerator.genSuccessResult(supplierService.save(supplier));
    }

    @DeleteMapping("/del/{id}")
    public Result delete(@PathVariable Integer id) {
        return ResultGenerator.genSuccessResult(supplierService.removeById(id));
    }

    @PutMapping("/update")
    public Result update(@RequestBody SupplierDTO supplierDTO) {
        if (ObjectUtils.isEmpty(supplierDTO)) {
            return ResultGenerator.genFailResult("数据不能为空");
        }
        Supplier supplier = new Supplier();
        BeanUtils.copyProperties(supplierDTO, supplier);
        supplier.setModifyDate(LocalDateTime.now());
        return ResultGenerator.genSuccessResult(supplierService.updateById(supplier));
    }

    @Transactional(rollbackFor = Exception.class)
    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        if (ObjectUtils.isEmpty(ids)) {
            return ResultGenerator.genFailResult("参数不能为空");
        }
        return ResultGenerator.genSuccessResult(supplierService.removeByIds(ids));
    }

    @GetMapping("/list/page")
    public Result<IPage> supplierPage(@RequestParam Map<String, Object> params) {
        QueryWrapper queryWrapper = new QueryWrapper();
        String isAsc = (String) params.get("isAsc");
        long pageNum = Integer.parseInt(params.getOrDefault("pageNum",1).toString());
        long pageSize = Integer.parseInt(params.getOrDefault("pageSize",10).toString());
        Page<Supplier> page = new Page<>(pageNum, pageSize);

        params.remove("isAsc");
        params.remove("pageNum");
        params.remove("pageSize");

        Long shopId = LoginContextUtil.instance().getEntryMicroShop().getId();
        params.put("shop_id", shopId);

        queryWrapper.allEq(params);
        queryWrapper.orderBy(false, false, isAsc);

        return ResultGenerator.genSuccessResult(supplierService.page(page, queryWrapper));
    }
}
